package com.hd.trans.ui.fragment;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.transition.ChangeBounds;
import android.transition.ChangeTransform;
import android.transition.Fade;
import android.transition.TransitionSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import androidx.lifecycle.Observer;

import com.hd.trans.R;
import com.hd.trans.TranslationInterior;
import com.hd.trans.db.bean.HuDunLanguage;
import com.hd.trans.framework.click.SingleClick;
import com.hd.trans.framework.click.XClickUtil;
import com.hd.trans.homefun.EventCode;
import com.hd.trans.ui.activity.SimulHomeActivity;
import com.hd.trans.ui.base.TransInit;
import com.hd.trans.ui.model.SimultaneousModel;
import com.hd.trans.utils.ToastUtils;

public class SimulNormalFragment extends SimulBaseFragment implements View.OnClickListener {
    private TextView topBt;
    private TextView bottomBt;
    private ImageView icBack;
    private TextView tvFrom;
    private TextView tvTo;
    private LinearLayout fromBt;
    private LinearLayout toBt;
    private ImageView exchange;
    private View ivMkf1,ivMkf2,rlRoot;
    private SimultaneousModel mDataModel;
    private SimulHomeActivity mActivity;
    private View.OnClickListener languageClick = new View.OnClickListener() {
        @SingleClick
        @Override
        public void onClick(View v) {
            if(XClickUtil.isFastDoubleClick(v,XClickUtil.INTERVAL_MILLIS)){
                return;
            }
            boolean isFromPage = true;
            int id = v.getId();
            if (id == R.id.left_bt) {
                mActivity.showLanguageDialog(true);
            } else if (id == R.id.right_bt) {
                mActivity.showLanguageDialog(false);
            } else if (id == R.id.icon_exchange) {
                showTranslateAnimation();
            }

        }
    };
    private TextView topTips;
    private TextView bottomTips;
    private RelativeLayout bottomLayout;
    private RelativeLayout topLayout;
    private View vtopTheme;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mActivity = (SimulHomeActivity) getActivity();
        View view = inflater.inflate(R.layout.activity_simultaneous_home, container, false);
        initView(view);
        setThemeColor();

        if(SimulHomeActivity.mOpenSceneTransition){ //共享元素动画
            mActivity.getWindow().setEnterTransition(new Fade());
            mActivity.getWindow().setExitTransition(new Fade());
            ViewCompat.setTransitionName(ivMkf1,getResources().getString(R.string.share_simultaneous_one));
            ViewCompat.setTransitionName(ivMkf2,getResources().getString(R.string.share_simultaneous_two));

            /**
             * 3、设置ShareElementTransition,指定的ShareElement会执行这个Transiton动画
             * 可设定ChangeBounds、ChangeTransform对象的属性值改变动画效果
             */
            TransitionSet transitionSet = new TransitionSet();
            transitionSet.addTransition(new ChangeBounds());
            transitionSet.addTransition(new ChangeTransform());
            transitionSet.addTarget(ivMkf1);
            transitionSet.addTarget(ivMkf2);
            mActivity.getWindow().setSharedElementEnterTransition(transitionSet);
            mActivity.getWindow().setSharedElementExitTransition(transitionSet);
        }
        ivMkf1.postDelayed(() -> {
            ivMkf1.setBackgroundColor(Color.TRANSPARENT);
        }, 500);
        ivMkf2.postDelayed(() -> {
            ivMkf2.setBackgroundColor(Color.TRANSPARENT);
        }, 500);
        return view;
    }

    private void setThemeColor() {
        topLayout.setBackgroundTintList(ColorStateList.valueOf(TranslationInterior.getThemeColor()));
        bottomLayout.setBackgroundTintList(ColorStateList.valueOf(TranslationInterior.getThemeColor()));
    }

    private void initView(View view) {
        icBack = view.findViewById(R.id.iv_left);
        topBt = view.findViewById(R.id.top_button);
        bottomBt = view.findViewById(R.id.bottom_button);
        topTips = view.findViewById(R.id.top_tip);
        bottomTips = view.findViewById(R.id.bottom_tip);
        bottomLayout = view.findViewById(R.id.bottom_layout);
        topLayout = view.findViewById(R.id.top_layout);
        vtopTheme = view.findViewById(R.id.iv_top_theme);
        vtopTheme.setBackgroundColor(TranslationInterior.getThemeColor());

        tvFrom = view.findViewById(R.id.tv_from);
        tvTo = view.findViewById(R.id.tv_to);
        fromBt = view.findViewById(R.id.left_bt);
        toBt = view.findViewById(R.id.right_bt);
        exchange = view.findViewById(R.id.icon_exchange);
        ivMkf1 = view.findViewById(R.id.iv_mkf1);
        ivMkf2 = view.findViewById(R.id.iv_mkf2);
        rlRoot = view.findViewById(R.id.rl_root);
        view.findViewById(R.id.iv_right).setVisibility(View.GONE);
        icBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ivMkf1.setBackgroundResource(R.drawable.shape_oval_blue);
                ivMkf2.setBackgroundResource(R.drawable.shape_oval_blue);
                getActivity().onBackPressed();
            }
        });
        topLayout.setOnClickListener(this);
        bottomLayout.setOnClickListener(this);
        fromBt.setOnClickListener(languageClick);
        exchange.setOnClickListener(languageClick);
        toBt.setOnClickListener(languageClick);
    }

    /**
     * 互换语种动画
     */
    private void showTranslateAnimation() {
        if("自动检测".equals(mDataModel.getLanguageFrom().getValue().getName())){
            ToastUtils.showNormal(getResources().getString(R.string.tips_to_not_auto));
            return;
        }
        TranslateAnimation ta1 = new TranslateAnimation(0, fromBt.getWidth() + exchange.getWidth(), 0, 0);
        ta1.setDuration(600);
        ta1.setFillAfter(true);
        fromBt.startAnimation(ta1);
        fromBt.bringToFront();

        TranslateAnimation ta2 = new TranslateAnimation(0, -toBt.getWidth()-exchange.getWidth(), 0, 0);
        ta2.setDuration(600);
        ta2.setFillAfter(true);
        toBt.startAnimation(ta2);
        toBt.bringToFront();

        ta1.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                HuDunLanguage fromTemp = mDataModel.getLanguageFrom().getValue();
                mDataModel.setLanguageFrom(mDataModel.getLanguageTo().getValue());
                mDataModel.setLanguageTo(fromTemp);
                animation.cancel();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        ta2.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                animation.cancel();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    @Override
    void onlazyLoad() {
        mDataModel = mActivity.mDataModel;
        mDataModel.getLanguageFrom().observe(this, new Observer<HuDunLanguage>() {
            @Override
            public void onChanged(HuDunLanguage huDunLanguage) {
                if (huDunLanguage != null) {
                    tvFrom.setText(huDunLanguage.getName());
                    topBt.setText(huDunLanguage.getShortName());
                    if(TransInit.getLanguageTipsMap() != null
                    && TransInit.getLanguageTipsMap().get(huDunLanguage.getName()) != null
                    && TransInit.getLanguageTipsMap().get(huDunLanguage.getName()).getTips() != null){
                        topTips.setText(TransInit.getLanguageTipsMap().get(huDunLanguage.getName()).getTips());
                    }else {
                        topTips.setText("点击按钮开始说" + huDunLanguage.getName());
                    }
                }
            }
        });
        mDataModel.getLanguageTo().observe(this, new Observer<HuDunLanguage>() {
            @Override
            public void onChanged(HuDunLanguage huDunLanguage) {
                if (huDunLanguage != null) {
                    tvTo.setText(huDunLanguage.getName());
                    bottomBt.setText(huDunLanguage.getShortName());
                    if(TransInit.getLanguageTipsMap() != null
                            && TransInit.getLanguageTipsMap().get(huDunLanguage.getName()) != null
                            && TransInit.getLanguageTipsMap().get(huDunLanguage.getName()).getTips() != null){
                        bottomTips.setText(TransInit.getLanguageTipsMap().get(huDunLanguage.getName()).getTips());
                    }else {
                        bottomTips.setText("点击按钮开始说" + huDunLanguage.getName());
                    }
                }
            }
        });

    }

    @Override
    public boolean onBackPressed() {
        ivMkf1.setBackgroundResource(R.drawable.shape_oval_blue);
        ivMkf2.setBackgroundResource(R.drawable.shape_oval_blue);

        if(SimulHomeActivity.mOpenSceneTransition){
            return false;
        }
        mActivity.finish();
        return true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mDataModel.getLanguageFrom().removeObservers(this);
        mDataModel.getLanguageTo().removeObservers(this);
    }

    @Override
    void onDataFresh() {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.top_layout) {
            if (mActivity.isLimit4Use(EventCode.SIMUL_TRANSLATE))
                mActivity.changeFragment(SimulHomeActivity.TOP);

        } else if (id == R.id.bottom_layout) {
            if (mActivity.isLimit4Use(EventCode.SIMUL_TRANSLATE)){
                HuDunLanguage fromTemp = mDataModel.getLanguageFrom().getValue();
                mDataModel.setLanguageFrom(mDataModel.getLanguageTo().getValue());
                mDataModel.setLanguageTo(fromTemp);
                mActivity.changeFragment(SimulHomeActivity.TOP);
            }

        }

    }
}
